using AntDesign;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using StudentVideo.Client.Common.Constants;
using StudentVideo.Client.Routers;
using StudentVideo.Shared.Dtos.Login;

namespace StudentVideo.Client.Pages;

public partial class Login {
   [Inject]
   public required ILocalStorageService _localStorage { get; set; }
   [Inject]
   public required IMessageService _message { get; set; }
   [Inject]
   public required LoginRouter _login { get; set; }
   [Inject]
   public required NavigationManager _navigationManager { get; set; }

   public enum ModulesEnum {
      Student,
      Teacher
   }

   private readonly LoginRequest form = new();
   private ModulesEnum module = ModulesEnum.Student;
   private Form<LoginRequest>? _form;

   private void ValueChanged(ModulesEnum value) {
      module = value;
      _message.Success(module.ToString());
   }

   private async void OnLogin() {
      if (_form is null)
         return;

      var validate = _form?.Validate();
      if (validate == false)
         return;

      var result = await _login.Admin(form);
      if (result.StatusCode == 200 && result.Data != null) {
         await _localStorage.SetItemAsync(LocalStorageKeys.Token, result.Data.Token);
         _message.Success("登录成功");

         _navigationManager.NavigateTo(PagePath.Home);
      } else {
         _message.Error("登录失败");
      }
   }
}
